package com.huawei.petSystemdemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huawei.petSystemdemo.R;
import com.huawei.petSystemdemo.Shop_Activity;
import com.huawei.petSystemdemo.bean.PetShop;
import com.huawei.petSystemdemo.bean.Pets;
import com.huawei.petSystemdemo.bean.Shop;
import com.huawei.petSystemdemo.data.PetRepository;
import com.huawei.petSystemdemo.utils.ImgRepository;
import com.huawei.petSystemdemo.utils.MyRequest;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ShopViewHolder> {

    List<Shop> shop_list;
    Context context;
    Map<String, Object> responseBody = new HashMap<>();

    public ShopAdapter(List<Shop> shop_list, Context context) {
        this.shop_list = shop_list;
        this.context = context;
    }

    public class ShopViewHolder extends RecyclerView.ViewHolder {

        ImageView shop_photo;
        TextView shop_name;
        TextView shop_add;
        TextView shop_num;
        TextView shop_province;
        TextView shop_city;
        RecyclerView shop_petRecycle;
        TextView shop_distance;


        public ShopViewHolder(@NonNull View itemView) {
            super(itemView);

            shop_photo = itemView.findViewById(R.id.shop_photo);
            shop_name = itemView.findViewById(R.id.shop_name);
            shop_add = itemView.findViewById(R.id.shop_add);
            shop_city = itemView.findViewById(R.id.shop_city);
            shop_province = itemView.findViewById(R.id.shop_province);
            shop_num = itemView.findViewById(R.id.shop_num);
            shop_photo = itemView.findViewById(R.id.shop_photo);
            shop_petRecycle = itemView.findViewById(R.id.shop_petRecycle);
            shop_distance = itemView.findViewById(R.id.shop_distance);

            GridLayoutManager manager = new GridLayoutManager(context, 3);
            shop_petRecycle.setLayoutManager(manager);

        }
    }

    @NonNull
    @Override
    public ShopViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.shop_comment, parent, false);
        return new ShopViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ShopViewHolder holder, int position) {
        Shop shop = shop_list.get(position);
        holder.shop_name.setText(shop.getName());
        holder.shop_add.setText(shop.getAdd());
        //holder.shop_photo.setImageResource(shop.getImage());
        int num = shop.getNum();
        holder.shop_num.setText(num + "");
        holder.shop_distance.setText("距离您 " + shop.getDistance() + "");

        List<Pets> list = new ArrayList<>();

        PetRepository.getByShopId(1, 3, shop.getId(), new Consumer<List<Pets>>() {
            @Override
            public void accept(List<Pets> pets) {
                list.addAll(pets);
                PetShopAdapter adapter = new PetShopAdapter(list);
                holder.shop_petRecycle.setAdapter(adapter);
            }
        });

        //附近商店页面进入商店页面
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = shop.getId();
                Intent intent = new Intent(context, Shop_Activity.class);
                intent.putExtra("shopId",id);
                context.startActivity(intent);
            }
        });



    }

    @Override
    public int getItemCount() {
        return shop_list.size();
    }
}
